#include "mytcpsocket.h"
#include "publicstruct.h"
#include "pyqwindow.h"
#include "ui_pyqwindow.h"

#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QResizeEvent>

PYQWindow::PYQWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PYQWindow)
{
    ui->setupUi(this);

    this->setWindowTitle("朋友圈");
    // 渲染窗口
    setQssStyle(QString("../style/PYQWindow.qss"),this);

    // 初始化发布动态窗口
    addfeedwindow = new AddFeedWindow;
    // 初始化评论列表窗口
    plwindow = new PLListWindow;

    level = 0;
    openNum = 1;
    animation = new QPropertyAnimation(plwindow, "pos");


    connect(this,SIGNAL(sig_newFeed(QVariant)),this,SLOT(addFeed(QVariant)));
    connect(this,SIGNAL(sig_newPL(QVariant)),this,SLOT(addPL(QVariant)));
    connect(this,SIGNAL(sig_newDZ(QVariant)),this,SLOT(addDZ(QVariant)));
    connect(this,SIGNAL(sig_unDZ(QVariant)),this,SLOT(removeDZ(QVariant)));

}

PYQWindow::~PYQWindow()
{
    delete ui;
}

void PYQWindow::Show()
{
    // 如果是第一次打开, 让服务器准备好 动态、评论、点赞 ,提醒我们拉取
    if(openNum == 1){
        int uid = GlobalObject::getGlobalObject()->getUserInfo().sdata.dd_num;

        QVariantMap map;
        map["cmd"] = "get_feed";
        map["timelineID"] = uid;

        //发送请求
        GlobalObject::getGlobalObject()->sendRequest(map);

        const char* ch = "$";
        GlobalObject::getGlobalObject()->getTcpsocket()->write(ch);


        QVariantMap map2;
        map2["cmd"] = "get_dzpl";
        map2["timelineID"] = uid;

        //发送请求
        GlobalObject::getGlobalObject()->sendRequest(map2);

        ++openNum;

    }
    show();
}

void PYQWindow::addFeed(QVariant var)
{
    struct st_feed feed = var.value<struct st_feed>();

    FeedItem *fitem = new FeedItem(feed,this);
    vec_feed.push_back(fitem);

    ui->contentLayout->insertWidget(level++,fitem);

    // 添加间距
    ui->contentLayout->addSpacing(10);

    connect(fitem,&FeedItem::sig_plwindow,this,[=](QVariant var){
        // 获取朋友圈窗口的 w,h,x,y(没有父窗口,返回基于屏幕的全局坐标)
        int w = this->width();
        int h = this->height();
        int x = this->pos().x();
        int y = this->pos().y();

        // 评论窗口(终点位置)
        int pl_w = w/3;
        int pl_h = h;
        int pl_x = x + w - pl_w;
        int pl_y = y;

        // 起始位置
        int pl_x_start = pl_x+pl_w;
        int pl_y_start = pl_y;


        QVector<QLabel*> vec = var.value<QVector<QLabel*>>();
        plwindow->addPls(vec);

        // 初始化位置和大小
        plwindow->setGeometry(pl_x,pl_y,pl_w,pl_h);
        // 初始隐藏
        plwindow->hide();

        plwindow->Show();

        // 创建动画效果
        animation->setDuration(300); // 动画持续时间
        animation->setStartValue(QPoint(pl_x_start, pl_y_start));
        animation->setEndValue(QPoint(pl_x, pl_y));
        animation->setEasingCurve(QEasingCurve::InOutQuint);
        animation->start();

    });

}

void PYQWindow::addPL(QVariant var)
{
    struct st_dzpl dzpl = var.value<struct st_dzpl>();
    // 判断这条评论属于哪个动态
    int index = 0;
    for(int i = 0;i<vec_feed.size();i++){
        if(vec_feed[i]->getFeed().feedID == dzpl.feedID){
            index = i;
            break;
        }
    }
    vec_feed[index]->addPL(dzpl.optime,dzpl.nick,dzpl.content);
}

void PYQWindow::addDZ(QVariant var)
{
    struct st_dzpl dzpl = var.value<struct st_dzpl>();
    // 判断这个点赞属于哪个动态
    int index = 0;
    for(int i = 0;i<vec_feed.size();i++){
        if(vec_feed[i]->getFeed().feedID == dzpl.feedID){
            index = i;
            break;
        }
    }

    vec_feed[index]->addDZ(dzpl.nick);

    // 对于已经点赞的动态，将点赞标识置为红心
    if(dzpl.nick == GlobalObject::getGlobalObject()->getUserInfo().sdata.nick)
        vec_feed[index]->setDZed();

}

void PYQWindow::removeDZ(QVariant var)
{
    struct st_dzpl dzpl = var.value<struct st_dzpl>();
    // 判断这个点赞属于哪个动态
    int index = 0;
    for(int i = 0;i<vec_feed.size();i++){
        if(vec_feed[i]->getFeed().feedID == dzpl.feedID){
            index = i;
            break;
        }
    }

    vec_feed[index]->removeDZ(dzpl.nick);
}

AddFeedWindow *PYQWindow::getAddfeedwindow() const
{
    return addfeedwindow;
}


void PYQWindow::on_btn_senddt_clicked()
{
    addfeedwindow->show();
}

